/*
 * @lc app=leetcode.cn id=98 lang=cpp
 *
 * [98] 验证二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    long long maxval = LONG_MIN;
    bool isValidBST(TreeNode *root)
    {
        //验证二叉搜索树 左小右大
        //题目已经限制节点个数大于等于1
        //中序遍历求解 实际上可以用一个数组保存  然后判断即可
        //这里还是用递归写
        if (root == NULL)
        {
            return true;
        }
        bool left = isValidBST(root->left);
        if (maxval < root->val)
        {
            maxval = root->val;
        }
        else
        {
            return false;
        }
        bool right = isValidBST(root->right);
        return left && right;
    }
};
// @lc code=end
